﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comom.Project.ControllerCom;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using FarmeWork.Helper;
using DataBase.Daihoctuonglai;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin")]
    public class VanphongdichvuController : UTController
    {
        //
        // GET: /Admin/Vanphongdichvu/

        private readonly IVanphongdichvu _iNews;
        private readonly int PageSize = 15;

        public VanphongdichvuController(IVanphongdichvu iNews)
        {
            this._iNews = iNews;
        }

        #region Tin chi tiết

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int page)
        {

            var listEntity = _iNews.List();
            List<VanphongdichvuModel> model = new List<VanphongdichvuModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    VanphongdichvuModel ml = new VanphongdichvuModel();
                    VanphongdichvuModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            var ListPageSize = new PagedData<VanphongdichvuModel>();

            if (model.Count() > 0)
            {
                ListPageSize.Data = model.Skip(PageSize * (page - 1)).Take(PageSize).ToList();
                ListPageSize.NumberOfPages = Convert.ToInt32(Math.Ceiling((double)model.Count() / PageSize));
                ListPageSize.CurrentPage = page;
            }
            else
            {
                ListPageSize.Data = null;
                ListPageSize.NumberOfPages = 0;
                ListPageSize.CurrentPage = 0;
            }

            return PartialView("_List", ListPageSize);
        }

        public ActionResult Add()
        {
            VanphongdichvuModel model = new VanphongdichvuModel();
            model.Id = 0;
            model.Datecreate = DateTime.Now;
            model.Active = true;
            return View("_Add", model);
        }

        public ActionResult Update(int id)
        {
            VanphongdichvuModel model = new VanphongdichvuModel();
            var entity = _iNews.GetById(id);
            try
            {
                VanphongdichvuModel.MapFrom(entity, ref model);
            }
            catch { }

            return PartialView("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(VanphongdichvuModel model)
        {

            try
            {

                if (string.IsNullOrEmpty(model.Title))
                {
                    return JsonError("Nhập tiêu đề văn phòng dịch vụ vào");
                }

                if (string.IsNullOrEmpty(model.ContentFull))
                {
                    return JsonError("Vui lòng nhập nội dung văn phòng dịch vụ vào");
                }

                var entity = new Van_phong_dich_vu();
                VanphongdichvuModel.MapFrom(model, ref entity);
                _iNews.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iNews.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        #endregion



    }
}
